package minh.bac.wcbrazil2014.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends ArrayAdapter<MatchScheduleObj>{

	Context context; 
	int resource;
	List<MatchScheduleObj> objects;
	DisplayImageOptions displayImageOptions;
	int typeShow;
	
	public ListViewAdapter(Context context, int resource,
			List<MatchScheduleObj> objects, DisplayImageOptions displayImageOptions, int typeShow ) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource  = resource;
		this.objects = objects;
		this.displayImageOptions  = displayImageOptions;
		this.typeShow = typeShow;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolder holderChild;
		if(convertView == null)
		{
			holderChild = new ViewHolder();
			convertView = inflater.inflate(resource, parent, false);			
			//
			holderChild.bang = (TextView) convertView.findViewById(R.id.bangdau_itemlistview);
			holderChild.doi1 = (TextView) convertView.findViewById(R.id.doi1_itemlistview);
			holderChild.time = (TextView) convertView.findViewById(R.id.time_itemlistivew);
			holderChild.doi2 = (TextView) convertView.findViewById(R.id.doi2_itemlistview);
			holderChild.imageDoi1 = (ImageView) convertView.findViewById(R.id.image_doi1);
			holderChild.imageDoi2 = (ImageView) convertView.findViewById(R.id.image_doi2);
			holderChild.scoredoi1 = (TextView) convertView.findViewById(R.id.score_doi1);
			holderChild.scoredoi2 = (TextView) convertView.findViewById(R.id.score_doi2);
			//
			convertView.setTag(holderChild);
		}
		else
			holderChild = (ViewHolder) convertView.getTag();
		if(typeShow == WCTAG.TYPE_SHOW_VONGBANG)
		{
			holderChild.bang.setText("Bảng " + ((VongBangObj)objects.get(position)).getBangDau());
		}
		else
			holderChild.bang.setText("Bảng " + ((VongDau)objects.get(position)).getVong());
		
		holderChild.doi1.setText(objects.get(position).getDoi1());
		holderChild.doi2.setText(objects.get(position).getDoi2());
		holderChild.time.setText(objects.get(position).getGio());
		String score1, score2;
		score1 = objects.get(position).getScore1();
		score2 = objects.get(position).getScore2();
		if(score1 == null)
		{
			score1 = "-";
		}
		if( score2 == null)
		{
			score2 = "-";
		}
		holderChild.scoredoi1.setText(score1);
		holderChild.scoredoi2.setText(score2);
		String urlImgDoi1, urlImgDoi2;
		urlImgDoi1 = objects.get(position).getImgLinkDoi1();
		urlImgDoi2 = objects.get(position).getImgLinkdoi2();
		if(urlImgDoi1 == null)
			urlImgDoi1 = "";
		if(urlImgDoi2 == null)
			urlImgDoi2 = "";
		Log.d("ADAPTER", urlImgDoi1 + "   " + urlImgDoi2);
		// load image
		ImageLoader.getInstance().displayImage(urlImgDoi1, holderChild.imageDoi1, displayImageOptions);
		ImageLoader.getInstance().displayImage(urlImgDoi2, holderChild.imageDoi2, displayImageOptions);
		return convertView;
	}
	
	private class ViewHolder{
		TextView bang, doi1, time, doi2, scoredoi1,scoredoi2;
		ImageView imageDoi1, imageDoi2;
	}

}
